/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-2-3 下午4:09 Created by dwzhang
 */
package com.jade.bss.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.jade.bss.base.BssConstants;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.CriterionUtils;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.vfile.VFileConstants;
import com.jade.framework.base.vfile.VFileUtils;
import com.jade.framework.base.util.PagingResult;
import com.jade.framework.base.util.ResponseUtils;
import com.jade.modules.config.ConfigEntity;
import com.jade.modules.config.ConfigUtils;
import com.jade.modules.software.Software;
import com.jade.modules.software.SoftwareUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 软件模块控制器。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a>
 * @version 1.0 15-2-3 下午4:09
 */
@RestController
@RequestMapping (value = "/software", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "软件管理", description = "软件管理", hidden = true)
public class SoftwareController
{
    private long maxFileSize = 20480000;

    public void setMaxFileSize(long maxFileSize)
    {
        this.maxFileSize = maxFileSize;
    }

    
    @RequiresAuthentication
    @RequestMapping (value = "/query", method = RequestMethod.GET)
    @ApiOperation(value = "查询软件包", httpMethod = "GET", hidden = true)
    public void querySoftware(@RequestParam (value = "type", required = false) String type,
                              @RequestParam (value = "keyword", required = false) String keyword,
                              @RequestParam (value = "offset") int offset,
                              @RequestParam (value = "length") int length,
                              @RequestParam (value = "sort", required = false) String sort,
                              @RequestParam (value = "order", required = false) String order,
                              HttpServletResponse response) throws Exception
    {
        Criteria criteria = new Criteria(offset, length);
        if (StringUtils.isNotBlank(type)) {
            criteria.add(Restrictions.eq("type", type));
        }
        if (StringUtils.isNotBlank(keyword)) {
            criteria.add(Restrictions.or(Restrictions.like("name", keyword),
                    Restrictions.like("version", keyword)));
        }
        CriterionUtils.addOrder(criteria, sort, order, "id");
        PagingList<Software> softwares = SoftwareUtils.querySoftware(criteria);
        Map<String,String> map = new HashMap<String,String>();
        List<ConfigEntity> entities = ConfigUtils.listEntity(BssConstants.OWNER_TYPE_SYSTEM,String.valueOf(BssConstants.OWNER_ID_SYSTEM), "software_type");
        for (ConfigEntity entity:entities){
            map.put(entity.getKey(),entity.getValue());
        }
        for (Software software : softwares) {
            if (StringUtils.isNotBlank(software.getFilePath())) {
                software.setFilePath(VFileUtils.getVFileURI(VFileConstants.DATA_TYPE_SOFTWARE, software.getFilePath()));
            }
        }
        PagingResult result = PagingResult.getResult(softwares);
        result.setParam("typeInfo",map);
        ResponseUtils.writeSuccessPagingResult(response, result);
    }

   
    @RequiresAuthentication
    @RequestMapping (value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "增加软件包", httpMethod = "POST", hidden = true)
    public void addSoftware(@RequestBody Software software,HttpServletResponse response)
            throws Exception
    {
        Date now = new Date();
        software.setCreationDate(now);
        SoftwareUtils.addSoftware(software);
        ResponseUtils.writeSuccessResult(response);
    }

   
    @RequestMapping (value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查看软件包", httpMethod = "GET", hidden = true)
    public void toViewDeviceSoftware(@PathVariable long id,HttpServletResponse response) throws Exception
    {
        Software software = SoftwareUtils.getSoftware(id);
        if (StringUtils.isNotBlank(software.getFilePath())) {
            software.setFilePath(VFileUtils.getVFileURI(VFileConstants.DATA_TYPE_SOFTWARE, software.getFilePath()));
        }
        ResponseUtils.writeSuccessResult(response,software);
    }

    @RequiresAuthentication
    @RequiresPermissions (value = {"software:edit"})
    @ApiOperation(value = "编辑软件包", httpMethod = "PUT", hidden = true)
    public void editSoftware(@RequestBody Software software,HttpServletResponse response)
            throws Exception
    {
        SoftwareUtils.updateSoftware(software);
        ResponseUtils.writeSuccessResult(response);
    }

   
    @RequiresAuthentication
    @RequestMapping (value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除软件包", httpMethod = "DELETE", hidden = true)
    public void removeSoftware(@RequestParam ("id") long[] ids, HttpServletResponse response)
            throws Exception
    {
        SoftwareUtils.removeSoftware(ids);
        ResponseUtils.writeSuccessResult(response);
    }
}
